package com.syh.jn.scm.domain.downstream.entity.purchase;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.downstream.repository.purchase.DownPurchaseOrderReadRepository;
import com.syh.jn.scm.dto.downstream.finance.DownStatementImgsDto;
import com.syh.jn.scm.dto.downstream.purchase.DownPurchaseReturnOrderMto;
import com.syh.jn.scm.dto.downstream.purchase.DownPurchaseReturnOrderSto;
import com.syh.jn.scm.dto.downstream.purchase.DownReturnGoodsDto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.validation.constraints.DecimalMin;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.UUID;

/**
 * Date:2018/7/2
 * Author:Clown
 * 采购退货单项实体;
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class DownPurchaseReturnOrderItem extends AbstractSimpleAggregate<Long> {

  //新增采购退货明细
  protected DownPurchaseReturnOrderItem(DownPurchaseReturnOrderSto.PurchaseRetOrdItemSTO itemSto) {
    this.uuid = UUID.randomUUID().toString();
    ExtBeanUtils.copyProperties(itemSto, this, false);
    //物流相关数量记录初始为0
    this.generatedQuantity = BigDecimal.ZERO;
    this.returnOutedQuantity = BigDecimal.ZERO;
    this.returnInQuantity = BigDecimal.ZERO;
    this.returnTotalAmount = this.price.multiply(this.returnQuantity).setScale(2, RoundingMode.HALF_UP);//退货总额
  }

  //修改采购退货明细
  protected DownPurchaseReturnOrderItem(DownPurchaseReturnOrderMto.PurchaseRetOrdItemMTO itemMto) {
    this.uuid = UUID.randomUUID().toString();
    ExtBeanUtils.copyProperties(itemMto, this, false);
    //物流相关数量记录初始为0
    this.generatedQuantity = BigDecimal.ZERO;
    this.returnOutedQuantity = BigDecimal.ZERO;
    this.returnInQuantity = BigDecimal.ZERO;
    this.returnTotalAmount = this.price.multiply(this.returnQuantity).setScale(2, RoundingMode.HALF_UP);//商品退货总额
  }

  public BigDecimal calculateTotalReturnOutAmount() {
    this.returnTotalOutAmount = this.price.multiply(this.returnOutedQuantity).setScale(2, RoundingMode.HALF_UP);//退货总额
    return this.returnTotalOutAmount;
  }

  /**
   * 归属id
   */
  private Long businessEntityId;
  /**
   * 销售退货单id
   */
  private Long saleReturnOrderId;
  /**
   * 订单Item外键
   */
  private Long orderItemId;
  /**
   * 商品id
   */
  private Long goodsId;
  /**
   * 商品名称
   */
  private String goodsName;
  /**
   * 分类id
   */
  private Long categoryId;
  /**
   * 品牌id
   */
  private Long goodsBrandId;
  /**
   * 品牌名称
   */
  private String goodsBrandName;
  /**
   * 商品编码
   */
  private String goodsCode;
  /**
   * 所属种类,应用分类编码
   */
  private String categoryCode;
  /**
   * 所属种类名称
   */
  private String categoryName;
  /**
   * 单位
   */
  private String units;
  /**
   * 条码
   */
  private String codeBar;
  /**
   * 规格
   */
  private String specName;
  /**
   * 所属种类,应用分类的父类目录
   */
  private String categoryCatalogCode;
  /**
   * -1 辅助商品  1  基础商品
   */
  private Short isDefaultGoods;
  /**
   * 与基础商品转换率（如果是基础商品 则等于1）
   */
  private BigDecimal conversionRate;
  /**
   * 单价
   */
  private BigDecimal price;
  /**
   * 商品数量
   */
  private BigDecimal quantity;
  /**
   * 商品发货数量
   */
  private BigDecimal outQuantity;
  /**
   * 商品收货数量
   */
  private BigDecimal inQuantity;
  /**
   * 退货订单数量
   */
  private BigDecimal returnQuantity;
  /**
   * 退货订单商品金额
   */
  private BigDecimal returnTotalAmount;
  /**
   * 退货单发货数量
   */
  private BigDecimal returnOutQuantity;
  /**
   * 物流单生成数量
   */
  private BigDecimal generatedQuantity;
  /**
   * 已出库的退货数量(发货)
   */
  private BigDecimal returnOutedQuantity;
  /**
   * 退货单发货商品金额
   */
  private BigDecimal returnTotalOutAmount;
  /**
   * 退货签收数量
   */
  private BigDecimal returnInQuantity;
  /**
   * 退货签收商品金额
   */
  private BigDecimal returnTotalInAmount;
  /**
   * 退货备注
   */
  private String remark;
  /**
   * 字典id(差异原因)
   */
  private Long returnReasonId;
}

